package com.atguigu.gmall.cart.api;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-17-18:58
 * @since 1.0.0
 */
@RequestMapping("/api/cart")
@RestController
public class CartController {



    @Autowired
    CartService cartService;

    @DeleteMapping("/deleteCart/{skuId}")//api/cart/deleteCart/40
    public Result ajaxDeleteCartItem(@PathVariable("skuId") Long skuId,
                             HttpServletRequest request){

        String key = cartService.determinCartKey(request);
        cartService.deleteCartItem(key,skuId);
        return Result.ok();
    }


    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result ajaxCheckCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("checked") Integer checked,
                                HttpServletRequest request){

        String key = cartService.determinCartKey(request);
        cartService.checkCartItem(key,skuId,checked);

        return Result.ok();
    }


    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result ajaxAddToCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("skuNum") Integer skuNum,
                                HttpServletRequest request){

        String key = cartService.determinCartKey(request);
        CartInfo cartInfo = cartService.addToCart(key, skuId, skuNum);
        return Result.ok(cartInfo);
    }


    /**
     * /api/cart/cartList
     * 返回购物车中所有的商品
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result listCart(HttpServletRequest request){

        String userCartKey = cartService.userCartKey(request);
        String tempCartKey = cartService.tempCartKey(request);

        //查询所有购物车数据
        List<CartInfo> cartInfos = cartService.listCart(userCartKey,tempCartKey);

        //把查到的购物车数据返回

        return Result.ok(cartInfos);
    }


    /**
     * 添加购物车
     * @param skuId 商品id
     * @param skuNum 商品名称
     * @param request 操作哪个用户的购物车
     * @return
     */
    @GetMapping("/addToCart")
    public CartInfo addToCart(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              HttpServletRequest request){


        String key = cartService.determinCartKey(request);
        CartInfo cartInfo = cartService.addToCart(key,skuId,skuNum);
        return cartInfo;
    }

    /**
     * 获取购物车中已经选中的商品
     * @return
     */
    @GetMapping("/cartListChecked")
    public List<CartInfo> cartINfoListCheck(HttpServletRequest request){
        String key = cartService.determinCartKey(request);
        return cartService.listCheckCartInfo(key);
    }
}
